package com.seafwg.multianno.mapper;

import com.seafwg.multianno.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2022/3/16 9:06 上午
 * @Version 1.0
 * @description 使用注解开发
 */

public interface UserMapper {
    // 使用注解实现用户的增删改查

    // 新增用户
    @Insert("insert into user values(#{id}, #{username}, #{password})")
    void addUser(User user);

    // 删除用户
    @Delete("delete from user where id=#{id}")
    void delUser(User user);

    // 修改用户
    @Update("update user set username=#{username}, password=#{password} where id=#{id}")
    void updateUser(User user);

    // 查询用户
    @Select("select * from user")
    List<User> findAllUser();

    // 通过uid查询用户
    @Select("select * from multi_user where id=#{id}")
    User findUserById(int id);

    // ============================= 多表查询 =====================================//
    // 查询用户，以及用户下所有的订单：一对多的关系
    @Select("select * from multi_user")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "birthday", column = "birthday"),
        @Result(
            property = "orderList",
            column = "id",
            javaType = List.class, // 注意类型
            many = @Many(select = "com.seafwg.multianno.mapper.OrderMapper.findOrderByUserId")
        )
    })
    List<User> findAllUserList();

    // 查询所有用户，以及该用户的角色
    @Select("select * from sys_user")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        // @Result(property = "birthday", column = "birthday"),
        @Result(
            property = "roleList",
            column = "id",
            javaType = List.class,
            // 多对多查询，通过传递参数，中间表和第三张表联合查询结果
            many = @Many(select = "com.seafwg.multianno.mapper.RoleMapper.findRoleByUserId")
        )
    })
    List<User> findAllUserAndRole();

}
